package com.qyhl.webtv.module_circle.circle.msg.action;

import android.util.Log;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.InteractionMessageBean;
import com.qyhl.webtv.module_circle.common.CircleUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by helin on 2018/1/26.
 */

public class InteractionModel implements InteractionContract.InteractionModel {

    private final InteractionContract.InteractionPresenter presenter;

    InteractionModel(InteractionContract.InteractionPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getInteractionMessage(int lastId, final int type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=interactList" + "&username=" + userName + "&lastMsgId=" + lastId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<List<InteractionMessageBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.getMessageError();
                    }

                    @Override
                    public void onSuccess(List<InteractionMessageBean> interactionMessageBeans) {
                        presenter.getMessageSuccess(interactionMessageBeans, type);
                    }
                });


    }

    /**
     * @param msgId 该参数暂时不用，现在全部取消标记
     */
    @Override
    public void clearMessageTip(int msgId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=flagInteract" + "&username=" + userName + "&msgId=" + msgId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            Log.i("clear_tips", "success");
                        }
                    }
                });

    }
}
